package kr.pe.johnkim.checkmileage.mobile.rest.controller;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMember;
import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMerchant;
import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMileage;
import kr.pe.johnkim.checkmileage.mobile.rest.service.*;
import kr.pe.johnkim.checkmileage.mobile.util.CheckMileageCommon;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Check Mileage 마일리지 관련 Controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @Date : 12. 7. 24
 */

@Controller
@Path("/checkMileageMileageController")
public class CheckMileageMileageController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(CheckMileageMileageController.class);

    @Autowired
    private CheckMileageMileageService checkMileageMileageService = null;

    @Autowired
    private CheckMileageMemberMileageLogService checkMileageMemberMileageLogService = null;

    @Autowired
    private CheckMileageMerchantService checkMileageMerchantService = null;

    @Autowired
    private CheckMileageMemberService checkMileageMemberService = null;

    @Autowired
    private CheckMileageCommon checkMileageCommon = null;

    /**
     * 기존에 가맹점에 등록된 사용자의 마일리지가 있는지 확인 한다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectMemberMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage selectMemberMileage(InputStream is) throws Exception {
        logger.info("selectMemberMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));

        logger.info("checkMileageMembersCheckMileageId : {}", parameterMileage.getCheckMileageMembersCheckMileageId());
        logger.info("checkMileageMerchantsMerchantId   : {}", parameterMileage.getCheckMileageMerchantsMerchantId());
        logger.info("activateYn                        : {}", parameterMileage.getActivateYn());

        return checkMileageMileageService.selectMemberMileage(parameterMileage);
    }

    /**
     * 마일리지를 등록 한다.
     *
     * 마일리지를 신규 등록하거나 적립할때 호출한다.
     *
     * 카드 결제의 경우.
     *
     * 1. 가맹점 요청에 의해서 카드/현금 결제 적용율을 분리 함.
     * 2. 현금 결제를 위한 registerMileageCash() 메소드 새로 추가 함.(20130227)
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/registerMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage registerMileage(InputStream is) throws Exception {
        logger.info("registerMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setMileage(jsonMileage2.getString("mileage"));
        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setModifyDate(jsonMileage2.getString("modifyDate"));
        parameterMileage.setRegisterDate(jsonMileage2.getString("registerDate"));
        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMileage.setBill(jsonMileage2.getString("bill"));

        // 마일리지 적용율을 가져 온다.
        CheckMileageMerchant parameterMerchant = new CheckMileageMerchant();
        parameterMerchant.setMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMerchant.setActivateYn(jsonMileage2.getString("activateYn"));

        // 카드 결제 적용율을 가져 온다.
        double decreaseMileage = Double.parseDouble(checkMileageMerchantService.selectMerchantInformation(parameterMerchant).getDecreaseMileage());
        double bill = Double.parseDouble(jsonMileage2.getString("bill"));

        // 결제금액과 적용율을 계산 한다.
        double doubleMileage = bill/decreaseMileage;

        // 소숫점 이하는 버린다.
        int intMileage = (int)doubleMileage;
        int oldMileage = Integer.parseInt(jsonMileage2.getString("mileage"));

        // 기존 마일리지와 합친다.
        int newMileage = intMileage + oldMileage;

        // 예전 마일리지 + 추가한 마일리지 = newMileage.
        logger.info("newMileage : {}", newMileage);

        parameterMileage.setMileage(Integer.toString(newMileage));
        parameterMileage.setAddMileage(Integer.toString(intMileage));
        // 마일리지 신규등록 또는 적립임을 명시한다.
        parameterMileage.setContent("AMASS");

        logger.info("mileage        : {}", parameterMileage.getMileage());
        logger.info("addMileage     : {}", parameterMileage.getAddMileage());   // 추가 또는 적립된 마일리지.
        logger.info("activateYn     : {}", parameterMileage.getActivateYn());
        logger.info("modifyData     : {}", parameterMileage.getModifyDate());
        logger.info("registerDate   : {}", parameterMileage.getRegisterDate());
        logger.info("checkMileageId : {}", parameterMileage.getCheckMileageMembersCheckMileageId());
        logger.info("merchantId     : {}", parameterMileage.getCheckMileageMerchantsMerchantId());
        logger.info("content        : {}", parameterMileage.getContent());

        CheckMileageMileage resultMileage = new CheckMileageMileage();

        try {
            // 만약 가맹점에 등록된 사용자가 아니면 마일리지를 새로 등록하고 맞으면 기존 마일리지를 업데이트 한다.
            if(("0").equals(checkMileageMileageService.checkMileageExist(parameterMileage).getTotalCount())) {
                logger.info("Not exist customer. Register new mileage for customer at merchant.");

                checkMileageMileageService.registerMileage(parameterMileage);
            } else {
                logger.info("Already exist customer. Update mileage for customer at merchant.");

                checkMileageMileageService.updateMileage(parameterMileage);
            }

            resultMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMileage.setResult("FAIL");
            logger.error("Fail to register mileage.");
        }

        try {
            /**
             * 사용자에게 Push Notification 을 보낸다.
             */
            CheckMileageMember parameterMember = new CheckMileageMember();

            parameterMember.setCheckMileageId(parameterMileage.getCheckMileageMembersCheckMileageId());
            parameterMember.setActivateYn("Y");

            CheckMileageMember resultMember = checkMileageMemberService.selectMemberInformation(parameterMember);

            String registrationId = resultMember.getRegistrationId();
            String receiveNotificationYn = resultMember.getReceiveNotificationYn();
            String deviceType = resultMember.getDeviceType();
            String languageCode = resultMember.getLanguageCode();
            String messageContents;

            logger.info("checkMileageId        : {}", parameterMember.getCheckMileageId());
            logger.info("activateYn            : {}", parameterMember.getActivateYn());
            logger.info("registrationId        : {}", registrationId);
            logger.info("receiveNotificationYn : {}", receiveNotificationYn);
            logger.info("deviceType            : {}", deviceType);
            logger.info("languageCode          : {}", languageCode);

            if((("AS").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // android mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendASMessage(registrationId, messageContents);
            } else if((("IP").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // iphone mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendIPMessage(registrationId, messageContents);
            }
        } catch (Exception e) {
            logger.error("Fail to push notification.");
        }

        return resultMileage;
    }

    /**
     * 마일리지를 등록 한다.
     *
     * 마일리지를 신규 등록하거나 적립할때 호출한다.
     *
     * 현금 결제의 경우.
     *
     * 1. 가맹점 요청에 의해서 카드/현금 결제 적용율을 분리 함.
     * 2. 현금 결제를 위한 registerMileageCash() 메소드 새로 추가 함.(20130227)
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/registerMileageCash")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage registerMileageCash(InputStream is) throws Exception {
        logger.info("registerMileageCash");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setMileage(jsonMileage2.getString("mileage"));
        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setModifyDate(jsonMileage2.getString("modifyDate"));
        parameterMileage.setRegisterDate(jsonMileage2.getString("registerDate"));
        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMileage.setBill(jsonMileage2.getString("bill"));

        // 마일리지 적용율을 가져 온다.
        CheckMileageMerchant parameterMerchant = new CheckMileageMerchant();
        parameterMerchant.setMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMerchant.setActivateYn(jsonMileage2.getString("activateYn"));

        // 현금 결제 적용율을 가져 온다.
        double decreaseMileage = Double.parseDouble(checkMileageMerchantService.selectMerchantInformation(parameterMerchant).getDecreaseMileageCash());
        double bill = Double.parseDouble(jsonMileage2.getString("bill"));

        // 결제금액과 적용율을 계산 한다.
        double doubleMileage = bill/decreaseMileage;

        // 소숫점 이하는 버린다.
        int intMileage = (int)doubleMileage;
        int oldMileage = Integer.parseInt(jsonMileage2.getString("mileage"));

        // 기존 마일리지와 합친다.
        int newMileage = intMileage + oldMileage;

        // 예전 마일리지 + 추가한 마일리지 = newMileage.
        logger.info("newMileage : {}", newMileage);

        parameterMileage.setMileage(Integer.toString(newMileage));
        parameterMileage.setAddMileage(Integer.toString(intMileage));
        // 마일리지 신규등록 또는 적립임을 명시한다.
        parameterMileage.setContent("AMASS");

        logger.info("mileage        : {}", parameterMileage.getMileage());
        logger.info("addMileage     : {}", parameterMileage.getAddMileage());   // 추가 또는 적립된 마일리지.
        logger.info("activateYn     : {}", parameterMileage.getActivateYn());
        logger.info("modifyData     : {}", parameterMileage.getModifyDate());
        logger.info("registerDate   : {}", parameterMileage.getRegisterDate());
        logger.info("checkMileageId : {}", parameterMileage.getCheckMileageMembersCheckMileageId());
        logger.info("merchantId     : {}", parameterMileage.getCheckMileageMerchantsMerchantId());
        logger.info("content        : {}", parameterMileage.getContent());

        CheckMileageMileage resultMileage = new CheckMileageMileage();

        try {
            // 만약 가맹점에 등록된 사용자가 아니면 마일리지를 새로 등록하고 맞으면 기존 마일리지를 업데이트 한다.
            if(("0").equals(checkMileageMileageService.checkMileageExist(parameterMileage).getTotalCount())) {
                logger.info("Not exist customer. Register new mileage for customer at merchant.");

                checkMileageMileageService.registerMileage(parameterMileage);
            } else {
                logger.info("Already exist customer. Update mileage for customer at merchant.");

                checkMileageMileageService.updateMileage(parameterMileage);
            }

            resultMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMileage.setResult("FAIL");
            logger.error("Fail to register mileage.");
        }

        try {
            /**
             * 사용자에게 Push Notification 을 보낸다.
             */
            CheckMileageMember parameterMember = new CheckMileageMember();

            parameterMember.setCheckMileageId(parameterMileage.getCheckMileageMembersCheckMileageId());
            parameterMember.setActivateYn("Y");

            CheckMileageMember resultMember = checkMileageMemberService.selectMemberInformation(parameterMember);

            String registrationId = resultMember.getRegistrationId();
            String receiveNotificationYn = resultMember.getReceiveNotificationYn();
            String deviceType = resultMember.getDeviceType();
            String languageCode = resultMember.getLanguageCode();
            String messageContents;

            logger.info("checkMileageId        : {}", parameterMember.getCheckMileageId());
            logger.info("activateYn            : {}", parameterMember.getActivateYn());
            logger.info("registrationId        : {}", registrationId);
            logger.info("receiveNotificationYn : {}", receiveNotificationYn);
            logger.info("deviceType            : {}", deviceType);
            logger.info("languageCode          : {}", languageCode);

            if((("AS").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // android mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendASMessage(registrationId, messageContents);
            } else if((("IP").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // iphone mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendIPMessage(registrationId, messageContents);
            }
        } catch (Exception e) {
            logger.error("Fail to push notification.");
        }

        return resultMileage;
    }

    /**
     * 마일리지를 업데이트 한다.
     *
     * 마일리지를 차감할때만 호출한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage updateMileage(InputStream is) throws Exception {
        logger.info("updateMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setMileage(jsonMileage2.getString("mileage"));
        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setModifyDate(jsonMileage2.getString("modifyDate"));
        parameterMileage.setRegisterDate(jsonMileage2.getString("registerDate"));
        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMileage.setSpendMileage(jsonMileage2.getString("spendMileage"));

        // 기존의 마일리지에서 사용하는 마일리지 값을 뺀다.
        int oldMileage = Integer.parseInt(jsonMileage2.getString("mileage"));
        int spendMileage = Integer.parseInt(jsonMileage2.getString("spendMileage"));
        int intMileage = oldMileage - spendMileage;

        parameterMileage.setMileage(Integer.toString(intMileage));
        // 마일리지 차감임을 명시 한다.
        parameterMileage.setContent("CHECK");

        logger.info("mileage        : {}", parameterMileage.getMileage());
        logger.info("activateYn     : {}", parameterMileage.getActivateYn());
        logger.info("modifyData     : {}", parameterMileage.getModifyDate());
        logger.info("checkMileageId : {}", parameterMileage.getCheckMileageMembersCheckMileageId());
        logger.info("merchantId     : {}", parameterMileage.getCheckMileageMerchantsMerchantId());
        logger.info("spendMileage   : {}", parameterMileage.getSpendMileage());     // 차감된 마일리지.
        logger.info("content        : {}", parameterMileage.getContent());

        CheckMileageMileage resultMileage = new CheckMileageMileage();

        try {
            checkMileageMileageService.updateMileage(parameterMileage);

            resultMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMileage.setResult("FAIL");
            logger.error("Fail to update mileage.");
        }

        try {
            /**
             * 사용자에게 Push Notification 을 보낸다.
             */
            CheckMileageMember parameterMember = new CheckMileageMember();

            parameterMember.setCheckMileageId(parameterMileage.getCheckMileageMembersCheckMileageId());
            parameterMember.setActivateYn("Y");

            CheckMileageMember resultMember = checkMileageMemberService.selectMemberInformation(parameterMember);

            String registrationId = resultMember.getRegistrationId();
            String receiveNotificationYn = resultMember.getReceiveNotificationYn();
            String deviceType = resultMember.getDeviceType();
            String languageCode = resultMember.getLanguageCode();
            String messageContents;

            logger.info("checkMileageId        : {}", parameterMember.getCheckMileageId());
            logger.info("activateYn            : {}", parameterMember.getActivateYn());
            logger.info("registrationId        : {}", registrationId);
            logger.info("receiveNotificationYn : {}", receiveNotificationYn);
            logger.info("deviceType            : {}", deviceType);
            logger.info("languageCode          : {}", languageCode);

            if((("AS").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // android mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendASMessage(registrationId, messageContents);
            } else if((("IP").equals(deviceType)) && (("Y").equals(receiveNotificationYn)) && (!("").equals(registrationId))) {
                // iphone mobile device 에 notification message 를 보낸다.
                if((languageCode).equals("ko")) {
                    messageContents = "캐럿이 변경되었습니다.";
                } else if((languageCode).equals("en")) {
                    messageContents = "Carrot has been changed.";
                } else {
                    messageContents = "Carrot has been changed.";
                }

                checkMileageCommon.sendIPMessage(registrationId, messageContents);
            }
        } catch (Exception e) {
            logger.error("Fail to push notification.");
        }

        return resultMileage;
    }

    /**
     * 사용자의 마일리지 목록을 가져 온다. 사용자 smart phone 에 출력.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectMemberMerchantMileageList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<CheckMileageMileage> selectMemberMerchantMileageList(InputStream is) throws Exception {
        logger.info("selectMemberMerchantMileageList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));

        return checkMileageMileageService.selectMemberMerchantMileageList(parameterMileage);
    }

    /**
     * 가맹점 고객의 마일리지 목록을 가져 온다. 가맹점 tablet PC/iPad 에 출력.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectMerchantMemberMileageList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<CheckMileageMileage> selectMerchantMemberMileageList(InputStream is) throws Exception {
        logger.info("selectMerchantMemberMileageList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));

        return checkMileageMileageService.selectMerchantMemberMileageList(parameterMileage);
    }

    /**
     * 마일리지를 비활성화 한다. 사용자가 서비스 탈퇴 시 관련 정보 비활성화.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateDeactivateMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage updateDeactivateMileage(InputStream is) throws Exception {
        logger.info("selectMerchantMemberMileageList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));
        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));

        CheckMileageMileage resultMileage = new CheckMileageMileage();

        try {
            checkMileageMileageService.updateDeactivateMileage(parameterMileage);
            resultMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMileage.setResult("FAIL");
            logger.error("Fail to update deactivate mileage.");
        }

        return resultMileage;
    }

    /**
     * 사용자의 QR 코드로 가맹점에 사용자가 등록되어 있는지 확인 한다.
     *
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/checkMileageExist")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMileage checkMileageExist(InputStream is) throws Exception {
        logger.info("updateMemberMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMileage = new JSONObject(line);
        JSONObject jsonMileage2 = jsonMileage.getJSONObject("checkMileageMileage");

        CheckMileageMileage parameterMileage = new CheckMileageMileage();

        parameterMileage.setCheckMileageMembersCheckMileageId(jsonMileage2.getString("checkMileageMembersCheckMileageId"));
        parameterMileage.setCheckMileageMerchantsMerchantId(jsonMileage2.getString("checkMileageMerchantsMerchantId"));
        parameterMileage.setActivateYn(jsonMileage2.getString("activateYn"));

        logger.info("checkMileageMembersCheckMileageId : {}", parameterMileage.getCheckMileageMembersCheckMileageId());
        logger.info("checkMileageMerchantsMerchantId   : {}", parameterMileage.getCheckMileageMerchantsMerchantId());
        logger.info("activateYn                        : {}", parameterMileage.getActivateYn());

        CheckMileageMileage resultMileage = new CheckMileageMileage();

        if((checkMileageMileageService.checkMileageExist(parameterMileage).getTotalCount()).equals("0")) {
            resultMileage.setMileage("0");
        } else {
            resultMileage = checkMileageMileageService.selectMemberMileage(parameterMileage);
        }

        return resultMileage;
    }
}
